package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.entity.*;
import com.mall.mapper.*;
import com.mall.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fire
 * @since 2020-04-16
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderproductMapper orderproductMapper;

    @Autowired
    private ProductMapper productMapper;
    
    @Autowired
    private OrderreturnMapper orderreturnMapper;
    
    @Autowired
    private UserMapper userMapper;
    
    @Override
    public int addOrder(int userId, String address, BigDecimal price) {
        Orders order = new Orders();
        order.setUserId(userId);
        order.setReceivingAddress(address);
        order.setPrice(price);
        order.setDeleted(0);
        ordersMapper.insert(order);
        return order.getId();
    }


    @Override
    public List<Orders> getOrdersByPage(int pageNo, int pageSize, int status) {
        Page<Orders> page = new Page<>(pageNo, pageSize);
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        if (status > -1) {
            wrapper.eq("status", String.valueOf(status));
        }
        wrapper.orderByDesc("create_time");
        ordersMapper.selectPage(page, wrapper);
        List<Orders> list = page.getRecords();
        for(Orders orders : list) {
            QueryWrapper<Orderreturn> returnWrapper = new QueryWrapper<>();
            returnWrapper.eq("order_id", orders.getId());
            orders.setOrderreturn(orderreturnMapper.selectOne(returnWrapper));
            QueryWrapper<User> userWrapper = new QueryWrapper<>();
            userWrapper.eq("id", orders.getUserId());
            orders.setUser(userMapper.selectOne(userWrapper));
            QueryWrapper<Orderproduct> wrapperProduct = new QueryWrapper<>();
            wrapperProduct.eq("order_id", orders.getId());
            wrapperProduct.select("product_id","count");
            List<Orderproduct> orderproductList = orderproductMapper.selectList(wrapperProduct);
            List<Product> productList = new ArrayList<>();
            for(Orderproduct o : orderproductList) {
                Product product = productMapper.selectById(o.getProductId());
                product.setCount(o.getCount());
                productList.add(product);
            }
            orders.setProductList(productList);
        }
        return list;
    }
    

    @Override
    public void deleteOrder(int orderId) {
        QueryWrapper<Orderproduct> wrapperProduct = new QueryWrapper<>();
        wrapperProduct.eq("order_id", orderId);
        orderproductMapper.delete(wrapperProduct);
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("id", orderId);
        ordersMapper.delete(wrapper);
    }

    
}
